package cn.dhnep.demo.service.impl;

import cn.dhnep.demo.mapper.HorsemanOrderMapper;
import cn.dhnep.demo.pojo.entity.HorsemanOrder;
import cn.dhnep.demo.pojo.entity.tbOrderItem;
import cn.dhnep.demo.service.HorsemanOrderService;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @Author 邱文彬
 * @Date 2021/11/17 9:51
 * @Version 1.0
 */

@Service
public class HorsemanOrderServiceImpl implements HorsemanOrderService {


    @Autowired
    private HorsemanOrderMapper horsemanOrderMapper;


    @Override
    public HorsemanOrder getHorsemanToday(String openId) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = formatter.format(getDayBegin(0));
        String finishTime = formatter.format(getDayEnd());
        HorsemanOrder hosermanOrder = new HorsemanOrder();
        int orderId = horsemanOrderMapper.getOrderId(startTime,finishTime, openId);
        hosermanOrder.setOrderQuantity(orderId);

        tbOrderItem horsemanToday = horsemanOrderMapper.getHorsemanTodayNo(0, startTime, finishTime, openId);
        if (horsemanToday != null) {
            hosermanOrder.setLargeObjectsPieces(horsemanToday.getAmount());
            hosermanOrder.setLargeObjectsPiecesAmount(horsemanToday.getSum());
        }
        tbOrderItem horsemanToday2 = horsemanOrderMapper.getHorsemanToday(0,startTime ,finishTime , openId);
        if (horsemanToday2 != null) {
            hosermanOrder.setReclaimedWeight(horsemanToday2.getFinallyWeight());
            hosermanOrder.setAmount(horsemanToday2.getSum());
        }


        return hosermanOrder;
    }

    @Override
    public HorsemanOrder getHorsemanThisWeek(String openId) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = formatter.format(getDayBegin(-7));
        String finishTime = formatter.format( getDayEnd());
        HorsemanOrder hosermanOrder = new HorsemanOrder();
        int orderId = horsemanOrderMapper.getOrderId(startTime, finishTime, openId);
        hosermanOrder.setOrderQuantity(orderId);

        tbOrderItem horsemanToday = horsemanOrderMapper.getHorsemanTodayNo(0, startTime, finishTime, openId);
        if (horsemanToday != null) {
            hosermanOrder.setLargeObjectsPieces(horsemanToday.getAmount());
            hosermanOrder.setLargeObjectsPiecesAmount(horsemanToday.getSum());
        }
        tbOrderItem horsemanToday2 = horsemanOrderMapper.getHorsemanToday(0, startTime, finishTime, openId);
        if (horsemanToday2 != null) {
            hosermanOrder.setReclaimedWeight(horsemanToday2.getFinallyWeight());
            hosermanOrder.setAmount(horsemanToday2.getSum());
        }


        return hosermanOrder;
    }

    @Override
    public HorsemanOrder getHorsemanThisMonth(String openId) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = formatter.format(getDayBegin(-30));
        String finishTime = formatter.format(getDayEnd());
        HorsemanOrder hosermanOrder = new HorsemanOrder();
        int orderId = horsemanOrderMapper.getOrderId(startTime,finishTime, openId);
        hosermanOrder.setOrderQuantity(orderId);
        System.out.println( startTime);
        System.out.println(finishTime);
        tbOrderItem horsemanToday = horsemanOrderMapper.getHorsemanTodayNo(0, startTime, finishTime, openId);
        if (horsemanToday != null) {
            hosermanOrder.setLargeObjectsPieces(horsemanToday.getAmount());
            hosermanOrder.setLargeObjectsPiecesAmount(horsemanToday.getSum());
        }
        tbOrderItem horsemanToday2 = horsemanOrderMapper.getHorsemanToday(0, startTime, finishTime, openId);
        if (horsemanToday2 != null) {
            hosermanOrder.setReclaimedWeight(horsemanToday2.getFinallyWeight());
            hosermanOrder.setAmount(horsemanToday2.getSum());
        }


        return hosermanOrder;
    }

    @Override
    public HorsemanOrder getHorsemanThisYear(String openId) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          String startTime = formatter.format(getDayBegin(-365));
        String finishTime = formatter.format(getDayEnd());
        HorsemanOrder hosermanOrder = new HorsemanOrder();
        int orderId = horsemanOrderMapper.getOrderId(startTime, finishTime, openId);
        hosermanOrder.setOrderQuantity(orderId);

        tbOrderItem horsemanToday = horsemanOrderMapper.getHorsemanTodayNo(0, startTime, finishTime, openId);
        if (horsemanToday != null) {
            hosermanOrder.setLargeObjectsPieces(horsemanToday.getAmount());
            hosermanOrder.setLargeObjectsPiecesAmount(horsemanToday.getSum());
        }
        tbOrderItem horsemanToday2 = horsemanOrderMapper.getHorsemanToday(0, startTime, finishTime, openId);
        if (horsemanToday2 != null) {
            hosermanOrder.setReclaimedWeight(horsemanToday2.getFinallyWeight());
            hosermanOrder.setAmount(horsemanToday2.getSum());
        }

        return hosermanOrder;
    }

    @Override
    public HorsemanOrder getHorsemanOrderByTime(@Param("openId") String openId, @Param("startTime") String startTime, @Param("finishTime") String finishTime) {

        HorsemanOrder hosermanOrder = new HorsemanOrder();
        int orderId = horsemanOrderMapper.getOrderId(startTime, finishTime, openId);
        hosermanOrder.setOrderQuantity(orderId);

        tbOrderItem horsemanToday = horsemanOrderMapper.getHorsemanTodayNo(0, startTime, finishTime, openId);
        if (horsemanToday != null) {
            hosermanOrder.setLargeObjectsPieces(horsemanToday.getAmount());
            hosermanOrder.setLargeObjectsPiecesAmount(horsemanToday.getSum());
        }
        tbOrderItem horsemanToday2 = horsemanOrderMapper.getHorsemanToday(0, startTime, finishTime, openId);
        if (horsemanToday2 != null) {

            hosermanOrder.setReclaimedWeight(horsemanToday2.getFinallyWeight());
            hosermanOrder.setAmount(horsemanToday2.getSum());
        }


        return hosermanOrder;
    }

    public static Date getDayBegin(int num) {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(cal.DATE, num);
        return cal.getTime();
    }

    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }
}
